/*
   ===============================================================================
   COPYRIGHT NOTICE

   Copyright (C) 2008 Daniel L. Heater
   International Copyright Secured.  All Rights Reserved.

   -------------------------------------------------------------------------------

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   o Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
   o Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
   o The names of its contributors may not be used to endorse or promote
   products derived from this software without specific prior written
   permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

   ===============================================================================
 */

#include <stdlib.h>
#include <string.h>
#include "symtab.h"

static symrec *sym_table = NULL;

static symrec * _putsym(symrec **sym_table, char *ident)
{
    symrec *ptr;

    ptr = (symrec *) malloc (sizeof (symrec));
    ptr->name = (char *) malloc (strlen (ident) + 1);
    if(ptr) {
        strcpy (ptr->name, ident);
        ptr->next = *sym_table;
        *sym_table = ptr;
    }

    return ptr;
}

int putsym_d(char *ident, double *ptr)
{
    symrec *sptr = _putsym(&sym_table, ident);

    sptr->value.d = ptr;
    sptr->sym_type = SYM_UD;

    return (NULL != sptr);
}

int putsym_i(char *ident, int *ptr)
{
    symrec *sptr = _putsym(&sym_table, ident);

    sptr->value.i = ptr;
    sptr->sym_type = SYM_UI;

    return (NULL != sptr);
}

int putsym_b(char *ident, int *ptr)
{
    symrec *sptr = _putsym(&sym_table, ident);

    sptr->value.b = ptr;
    sptr->sym_type = SYM_UB;

    return (NULL != sptr);
}

symrec * getsym(char *ident)
{
    symrec *ptr;

    for (ptr = sym_table; ptr; ptr = ptr->next) {
        if (0 == strcmp (ptr->name, ident)) {
            return ptr;
        }
    }

    return NULL;
}

#if 0
static symrec * _getsym(symrec *sym_table, char *ident)
{
    symrec *ptr;

    for (ptr = sym_table; ptr; ptr = ptr->next) {
        if (0 == strcmp (ptr->name, ident)) {
            return ptr;
        }
    }

    return NULL;
}

dexpr_t *getsym_dx(char *ident)
{
    dexpr_t *ptr;
    symrec *sptr = _getsym(dx_sym_table, ident);

    if(sptr) {
        ptr = sptr->dx;
        return ptr;
    }

    return NULL;
}

iexpr_t *getsym_ix(char *ident)
{
    iexpr_t *ptr;
    symrec *sptr = _getsym(ix_sym_table, ident);

    if(sptr) {
        ptr = sptr->ix;
        return ptr;
    }

    return NULL;
}

double *getsym_d(char *ident)
{
    double *ptr;
    symrec *sptr = _getsym(d_sym_table, ident);

    if(sptr) {
        ptr = sptr->d;
        return ptr;
    }

    return NULL;
}

int *getsym_i(char *ident)
{
    int *ptr;
    symrec *sptr = _getsym(i_sym_table, ident);

    if(sptr) {
        ptr = sptr->i;
        return ptr;
    }

    return NULL;
}
#endif
